package org.bbb.library1.editors;

/*******************************************************************************
 * Copyright (c) 2007 Finalist IT Group B.V.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Rob Schellhorn - initial API and implementation
 *******************************************************************************/
//package com.finalist.rcp.internal.ui.editors;

import static org.eclipse.ui.forms.widgets.ExpandableComposite.EXPANDED;
import static org.eclipse.ui.forms.widgets.ExpandableComposite.TITLE_BAR;
import org.eclipse.jface.dialogs.IMessageProvider;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.forms.IManagedForm;
import org.eclipse.ui.forms.IMessageManager;
import org.eclipse.ui.forms.SectionPart;
import org.eclipse.ui.forms.editor.FormEditor;
import org.eclipse.ui.forms.editor.FormPage;
import org.eclipse.ui.forms.events.HyperlinkAdapter;
import org.eclipse.ui.forms.events.HyperlinkEvent;
import org.eclipse.ui.forms.widgets.FormText;
import org.eclipse.ui.forms.widgets.FormToolkit;
import org.eclipse.ui.forms.widgets.ScrolledForm;
import org.eclipse.ui.forms.widgets.Section;

import org.bbb.library1.ImageConstants;
import org.bbb.library1.RcpDemoPlugin;
import org.bbb.library1.model.Book;

/**
 * @author Rob Schellhorn
 */
public class OverviewFormPage extends FormPage {

   private class BookIndexSectionPart extends SectionPart {

      BookIndexSectionPart(Composite parent, FormToolkit toolkit) {
         super(parent, toolkit, EXPANDED | TITLE_BAR);
      }

      /*
       * @see org.eclipse.ui.forms.AbstractFormPart#initialize(org.eclipse.ui.forms.IManagedForm)
       */


   }

   private class GeneralInformationSectionPart extends SectionPart {
      //private Text numberOfPages;
      //private Text publisher;
      private Text title;
      private Text author;

      GeneralInformationSectionPart(Composite parent, FormToolkit toolkit) {
         super(parent, toolkit, EXPANDED | TITLE_BAR | Section.DESCRIPTION);
      }

      /*
       * @see org.eclipse.ui.forms.AbstractFormPart#commit(boolean)
       */
      @Override
      public void commit(boolean onSave) {
         if (onSave) {
            Book book = getEditorInput().getBook();
            //book.setPublisher(publisher.getText());
            book.setTitle(title.getText());
            book.setAuthor(author.getText());
         }

         super.commit(onSave);
      }

      /*
       * @see org.eclipse.ui.forms.AbstractFormPart#initialize(org.eclipse.ui.forms.IManagedForm)
       */
      @Override
      public void initialize(IManagedForm form) {
         super.initialize(form);

         Section section = getSection();
         section.setText("General Information");
         section.setDescription("Enter book title below");
         section.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

         FormToolkit toolkit = form.getToolkit();
         Composite client = toolkit.createComposite(section);
         client.setLayout(new GridLayout(2, false));
         section.setClient(client);

         final IMessageManager manager = form.getMessageManager();

         toolkit.createLabel(client, "Title:");
         title = toolkit.createText(client, null);
         title.addModifyListener(new ModifyListener() {
            public void modifyText(ModifyEvent e) {
               String value = title.getText().trim();
               if (value.length() > 0) {
                  manager.removeMessage("title", title);
               } else {
                  manager.addMessage("title", "The title must be at least one character long",
                        null, IMessageProvider.ERROR, title);
               }

               markDirty();
            }
         });
         title.setLayoutData(new GridData(SWT.FILL, SWT.BEGINNING, true, false));
         
         toolkit.createLabel(client, "Author:");
         author = toolkit.createText(client, null);
         author.addModifyListener(new ModifyListener() {
            public void modifyText(ModifyEvent e) {
               String value = author.getText().trim();
               if (value.length() > 0) {
                  manager.removeMessage("author", author);
               } else {
                  manager.addMessage("author", "The author must be at least one character long",
                        null, IMessageProvider.ERROR, author);
               }

               markDirty();
            }
         });
         author.setLayoutData(new GridData(SWT.FILL, SWT.BEGINNING, true, false));
      }
 

      /*
       * @see org.eclipse.ui.forms.AbstractFormPart#refresh()
       */
      @Override
      public void refresh() {
         Book book = getEditorInput().getBook();
         //publisher.setText(book.getPublisher());
         title.setText(book.getTitle());
         author.setText(book.getAuthor());

         //Integer pages = book.getNumberOfPages();
         //numberOfPages.setText(pages != null ? String.valueOf(pages) : "");

         super.refresh();
      }
   }



   private final String FORM_TEXT = "<form><p>The contents of a book is made up of the following sections:</p>"
         + "<li style=\"image\" value=\"image\"><a href=\"authors\" nowrap=\"true\">Authors</a>: lists all the books authors.</li>"
         + "</form>";

   public OverviewFormPage(FormEditor editor) {
      super(editor, "overview", "Overview");
   }

   /*
    * @see org.eclipse.ui.forms.editor.FormPage#createFormContent(org.eclipse.ui.forms.IManagedForm)
    */
   @Override
   protected void createFormContent(IManagedForm managedForm) {
      ScrolledForm form = managedForm.getForm();
      form.setImage(RcpDemoPlugin.getDefault().getImageRegistry().get(ImageConstants.BOOK_ICON));
      form.setText("Overview");

      FormToolkit toolkit = managedForm.getToolkit();
      toolkit.decorateFormHeading(form.getForm());

      Composite body = form.getBody();
      GridLayout layout = new GridLayout(2, true);
      layout.marginHeight = 10;
      layout.marginWidth = 6;
      layout.horizontalSpacing = 20;
      body.setLayout(layout);

      managedForm.addPart(new GeneralInformationSectionPart(body, toolkit));
      //managedForm.addPart(new BookIndexSectionPart(body, toolkit));
      //managedForm.addPart(new GenresSectionPart(body, toolkit));
      //managedForm.addPart(new SummarySectionPart(body, toolkit));
   }

   /*
    * @see org.eclipse.ui.part.EditorPart#getEditorInput()
    */
   @Override
   public BookEditorInput getEditorInput() {
      return (BookEditorInput) super.getEditorInput();
   }
}
